package top.kaoshanji.p3ch13a;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单-p3ch13a-235页a：使用请求包装器的AutoCorrect过滤器
 * @author kaoshanji
 * ^_^
 * create time 2025/4/9 14:21
 */
@WebFilter(filterName = "AutoCorrectFilter", urlPatterns = {"/*"})
public class AutoCorrectFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        // 设置请求对象的编码
        httpServletRequest.setCharacterEncoding(StandardCharsets.UTF_8.name());

        AutocorrectHttpServletRequestWrapper wrapper = new AutocorrectHttpServletRequestWrapper(httpServletRequest);

        // 传入的是包装器请求对象...虽然还是请求对象，但是，这是被加了料的
        chain.doFilter(wrapper, response);

    }


}
